layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;


    //获取当前用户id和部门
    var userId = '';
    var department = '';
    $.ajax(Feng.ctxPath + "/databaseListKh/getUser", {
        async: false,
        success: function (res) {
            // console.log(res.data)
            userId = res.data.id
            department = res.data.deptName
        }
    })

    $(function () {
        insertBtn();
    })

    //申请按钮和第二次点进来按钮禁用
    function insertBtn() {
        var a = '<div class="layui-col-md2 layui-col-md-offset1">';
        a += '<button class="layui-btn layui-btn-normal" id="apply">&nbsp;申请&nbsp;</button>';
        a += '</div>';

        var b = '<div class="layui-col-md2 layui-col-md-offset1">';
        b += '<button class="layui-btn layui-btn-normal layui-btn-disabled" disabled>&nbsp;已申请&nbsp;</button>';
        b += '</div>';
        $("#btn").empty();


        $.ajax(Feng.ctxPath + '/interfaceapproval/list', {
            data: {
                userId: userId,
                echo: 1
            },
            success: function (data) {
                console.log(data)
                var c = ''
                if (data.data.length != 0) {
                    if (data.data[0].state == 1) {
                        c += '<div class="layui-col-md12" style="margin-top: 35px">';
                        c += '<span class="layui-col-md1" style="text-align: right">状态：</span>';
                        c += '<span class="layui-col-md5" style="margin-left: 20px">' + '已审核' + '</span>';
                        c += '</div>';
                        c += '<div class="layui-col-md12" style="margin-top: 35px">';
                        c += '<span class="layui-col-md1" style="text-align: right">结果反馈：</span>';
                        c += '<span class="layui-col-md5" style="margin-left: 20px">' + '审核通过,接口正在开发中......' + '</span>';
                        c += '</div>';
                    } else if (data.data[0].state == 0) {
                        c += '<div class="layui-col-md12" style="margin-top: 35px">';
                        c += '<span class="layui-col-md1" style="text-align: right">状态：</span>';
                        c += '<span class="layui-col-md5" style="margin-left: 20px">' + '已申请' + '</span>';
                        c += '</div>';
                        c += '<div class="layui-col-md12" style="margin-top: 35px">';
                        c += '<span class="layui-col-md1" style="text-align: right">结果反馈：</span>';
                        c += '<span class="layui-col-md5" style="margin-left: 20px">' + '等待审批中......' + '</span>';
                        c += '</div>';
                    }
                    $('#description').val(data.data[0].description)
                    $('#description').attr('disabled', true)
                    $('#system').val(data.data[0].system)
                    $('#system').attr('disabled', true)
                    $('#display').append(c)
                    $('#btn').append(b)
                } else {
                    $('#description').attr('disabled', false)
                    $('#system').attr('disabled', false)
                    $('#btn').append(a)
                }
            }
        })
    }


    //点击申请
    $('#btn').on('click', '#apply', function () {
        if ($('#description').val() == "" || $('#system').val() == "") {
            Feng.error("请填写必填项")
            return
        }
        $.ajax(Feng.ctxPath + '/interfaceapproval/addItem', {
            data: {
                userId: userId,
                department: department,
                content: 2,
                description: $('#description').val(),
                state: 0,
                servicetype: 1,
                system: $('#system').val(),
                echo: 1
            },
            async: false,
            success: function (data) {
                console.log(data)
                // $("#echo").attr("value",'echo')
                history.go(-1)
            }
        })
    })

    //返回按钮
    $("#goBack").click(function () {
        history.go(-1)
    })

});
